package com.zhangyu.shadow.repair.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.zhangyu.shadow.repair.R;
import com.zhangyu.shadow.repair.aop.anotations.LoginFilter;
import com.zhangyu.shadow.repair.config.RequestUrls;
import com.zhangyu.shadow.repair.delegate.login.LoginDelegate;
import com.zhangyu.shadow.repair.delegate.luncher.LauncherDelegate;
import com.zhangyu.shadow.repair.event.LogoutEvent;
import com.zhangyu.shadow.repair.event.StartBrotherEvent;
import com.zhangyu.shadow.repair.event.TokenInvalidEvent;
import com.zhangyu.shadow.repair.manager.AccountManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import st.lib.base.account.AccountChecker;
import st.lib.base.delegate.RxShadowDelegate;
import st.lib.base.net.callback.IError;
import st.lib.base.net.callback.ISuccess;
import st.lib.base.net.rx.RxRestClient;
import st.lib.base.ui.progress.sweetalert.DialogLoader;
import st.lib.base.ui.progress.sweetalert.IDialogClickListener;

/**
 * Description:
 *
 * @author Hequn.Lee
 * @date 2018/11/2
 */
public class RootDelegate extends RxShadowDelegate {

    public static RootDelegate newInstance() {
        return new RootDelegate();
    }

    private DialogLoader mDialogLoader;

    @Override
    public Object setLayout() {
        return R.layout.delegate_root;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mDialogLoader = new DialogLoader(_mActivity);
        EventBus.getDefault().register(this);
        RxRestClient.builder()
                .url(RequestUrls.URL_CAN_EDIT_START_ADN_END_TIME)
                .params(AccountManager.getInstance().getBaseInfo())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        AccountManager.getInstance().setCanEditTime(response);
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {

                    }
                })
                .build()
                .post();
    }

    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        super.onDestroyView();

    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        lib_login_filter();
    }

    @LoginFilter
    private void lib_login_filter() {
        loadRootFragment(R.id.fl_first_container, LauncherDelegate.newInstance());
    }

    @Subscribe
    public void onLogoutEvent(LogoutEvent event) {
        startWithPop(LoginDelegate.newInstance());
    }

    @Subscribe
    public void startBrother(StartBrotherEvent event) {
        start(event.mISupportFragment);
    }

    @Subscribe
    public void onTokenInvalid(TokenInvalidEvent event) {
        mDialogLoader.showFailed("登录过期", "请重新登录", new IDialogClickListener() {
            @Override
            public void onClick() {
                mDialogLoader.clearDialog();
                AccountChecker.setSignState(false);
                startWithPop(LoginDelegate.newInstance());
            }
        });
    }
}
